package com.example.shushiyunsst.fragment.main.home.child.new_sale.chlid

import android.os.Bundle
import android.view.View
import android.widget.Toast
import androidx.appcompat.widget.Toolbar
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.alibaba.fastjson.JSON
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.listener.OnItemChildClickListener
import com.example.library_common.adapter.JssBaseViewHolder
import com.example.library_common.album.utils.Utils
import com.example.library_common.base.BaseFragmentEx
import com.example.library_common.base.SimpleListFragment
import com.example.library_common.bean.CommonBean
import com.example.library_common.networking.RestClient
import com.example.shushiyunsst.R
import com.example.shushiyunsst.fragment.main.home.child.miner.bean.StartTransferFormBean
import com.example.shushiyunsst.fragment.main.home.child.new_sale.chlid.bean.CollectBean
import com.example.shushiyunsst.util.COLLECT
import com.example.shushiyunsst.util.GOODS_DETAIL
import com.example.shushiyunsst.util.GOODS_ID
import com.example.shushiyunsst.util.startNavigation
import com.example.wallet.db.UserInfoManager
import ikidou.reflect.TypeBuilder
import java.lang.reflect.Type

/**
 * 收藏
 * */
@Route(path = COLLECT)
class CollectFragment : SimpleListFragment<CollectBean>(), OnItemChildClickListener {

    companion object {
        fun newInstance(): CollectFragment {
            val args = Bundle()

            val fragment = CollectFragment()
            fragment.arguments = args
            return fragment
        }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        isAddDividerItemDecoration = false
//        isEnableLoadMore = false
    }


    override fun initView(view: View) {
        super.initView(view)
        view.setBackgroundResource(R.color.bg_color)
        mHeader.removeAllViews()
        addHeader(R.layout.comment_header_layout)
        mHeader.setBackgroundResource(R.color.bg_color)
        val mToolbar: Toolbar = mHeader.findViewById(R.id.mToolbar)
        mToolbar.setTitle(R.string.my_collect)
        mToolbar.setNavigationOnClickListener { onBackPressedSupport() }

        mAdapter.addChildClickViewIds(R.id.deleteTv)
        mAdapter.setOnItemChildClickListener(this)
    }

    override fun getItemLayout() = R.layout.shou_cang_fragment

    override fun convertItem(helper: JssBaseViewHolder?, item: CollectBean?) {
        helper?.setText(R.id.tile_tv, item?.goodsName)
            ?.setImageNetUrl(R.id.avatar, item?.goodsLogo)
    }

    override fun getListType(): Type {
        return TypeBuilder.newInstance(CommonBean::class.java)
            .beginSubType(List::class.java)
            .addTypeParam(CollectBean::class.java).endSubType().build()
    }

    override fun netRequest() {
//        val jsonFromRaw = Utils.getJsonFromRaw(_mActivity, R.raw.temp_fack_data)
//        parseDate(jsonFromRaw)

//        val jsonFromRaw = ""
//        parseDate(jsonFromRaw)

        val userInfo = UserInfoManager.userInfoManager.getUserInfoWrapper()?.userInfo
        val maps = mapOf(
            "pageCount" to Int.MAX_VALUE,
            "pageIndex" to pageIndex,
            "uid" to userInfo?.uid
        )

        RestClient.builder()
            .url(R.string.queryMyColletcList)
            .raw(JSON.toJSONString(maps))
            .success {
                parseDate(it)

            }.failure {

                onFailed()
            }.error { code, msg ->
                onFailed()
            }
            .build()
            .post()
    }


    override fun parseDate(data: String?) {
//        super.parseDate(data)

        val obj = JSON.parseObject(data)
        val collectList = obj["collectList"]
        val array = JSON.parseArray(collectList.toString(), CollectBean::class.java)
        loadListDate(array)
    }

    override fun onItemChildClick(adapter: BaseQuickAdapter<*, *>, view: View, position: Int) {
        val item = mAdapter.getItem(position)
        val maps = mapOf(
            "goodsId" to item.goodsId,
            "uid" to item?.uid,
            "opt" to 0,
        )

        RestClient.builder()
            .url(R.string.saveOrCancelCollect)
            .raw(JSON.toJSONString(maps))
            .success {
                mAdapter.removeAt(position)
                if (mAdapter.itemCount <= 0) {
                    if (!mSwipeRefreshLayout.isRefreshing) {
                        mSwipeRefreshLayout.isRefreshing = true
                    }
                    onRefresh()
                }
            }
            .failure {
                Toast.makeText(_mActivity, "发送失败", Toast.LENGTH_LONG).show()
            }
            .error { _, msg ->
                Toast.makeText(_mActivity, msg, Toast.LENGTH_LONG).show()
            }
            .build()
            .post()


    }

    override fun onItemClick(adapter: BaseQuickAdapter<*, *>, view: View, position: Int) {
        super.onItemClick(adapter, view, position)
        val item = mAdapter.getItem(position)
        ARouter.getInstance()
            .build(GOODS_DETAIL)
            .withString(GOODS_ID, item?.cid.toString())
            .startNavigation(this)
    }

}